package pt.ua.helpdesk.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;

import persistence.Administrator;
import persistence.Assistant;
import persistence.Command;
import persistence.Faq;
import pt.ua.helpdesk.client.ServiceFaq;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ServiceFaqImpl extends RemoteServiceServlet implements ServiceFaq {
	
	private static final long serialVersionUID = 1L;

	@Override
	public Boolean addFaq(Integer idPerson, String question, String answer) {
		Boolean ret = false;
		
		try{
		Assistant a = (Assistant)Command.find(Assistant.class, idPerson);
		
		Faq f = new Faq();
		f.setAccepted((short)0);
		f.setActive((short)1);
		f.setDate(new Date());
		f.setFaq(answer);
		f.setRefidpersonAdmCollection(new ArrayList<Administrator>());
		f.setRefidpersonAssistant(a);
		f.setTitle(question);
		
		a.getFaqCollection().add(f);
		
		ret =  new Boolean(Command.update(a));
		
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return ret;
	}

	@Override
	public ArrayList getFaq() {
		
		try{
		Vector<Faq> f;
		ArrayList<String> i = new ArrayList<String>();
		ArrayList<String> t = new ArrayList<String>();
		ArrayList<String> a = new ArrayList<String>();
		
		ArrayList<ArrayList<String>> ret = new ArrayList<ArrayList<String>>();
		
		String sql = "";
		sql += "select f from Faq f where (f.active = ?1) AND (f.accepted = ?2)";
		
		ArrayList<Integer> par = new ArrayList<Integer>();
		par.add(new Integer(1));
		par.add(new Integer(1));
		
		f = (Vector<Faq>)Command.select(sql, par);
		
		for(int k = 0; k< f.size(); k++)
		{
			i.add(f.get(k).getIdfaq().toString());
			t.add(f.get(k).getTitle());
			a.add(f.get(k).getRefidpersonAssistant().getUsername());
		}
		
		ret.add(i);
		ret.add(t);
		ret.add(a);
		
		return ret;
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public ArrayList viewFaq(Integer idFaq) {
		// TODO Auto-generated method stub
		
		ArrayList ret = new ArrayList();
		
		Faq f = (Faq)Command.find(Faq.class, idFaq);
		
		ret.add(f.getRefidpersonAssistant().getUsername());
		ret.add(f.getDate().toLocaleString());
		ret.add(f.getTitle());
		ret.add(f.getFaq());
		
		return ret;
	}

	@Override
	public ArrayList getFaqAll() {
		
		try{
		Vector<Faq> f;
		ArrayList<String> i = new ArrayList<String>();
		ArrayList<String> t = new ArrayList<String>();
		ArrayList<String> a = new ArrayList<String>();
		ArrayList<String> c = new ArrayList<String>();
		
		ArrayList<ArrayList<String>> ret = new ArrayList<ArrayList<String>>();
		
		String sql = "";
		sql += "select f from Faq f where (f.active = ?1)";
		
		ArrayList<Integer> par = new ArrayList<Integer>();
		par.add(new Integer(1));
		
		f = (Vector<Faq>)Command.select(sql, par);
		
		for(int k = 0; k< f.size(); k++)
		{
			i.add(f.get(k).getIdfaq().toString());
			t.add(f.get(k).getTitle());
			a.add(f.get(k).getRefidpersonAssistant().getUsername());
			c.add(new Integer(f.get(k).getAccepted()).toString());
		}
		
		ret.add(i);
		ret.add(t);
		ret.add(a);
		ret.add(c);
		
		return ret;
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Boolean acceptFaq(Integer idFaq) {
		Faq f = (Faq)Command.find(Faq.class, new Integer(idFaq));
		f.setAccepted((short)1);
		
		return new Boolean(Command.update(f));
	}

	@Override
	public Boolean removeFaq(Integer idFaq) {
		Faq f = (Faq)Command.find(Faq.class, new Integer(idFaq));
		f.setActive((short)0);
		
		return new Boolean(Command.update(f));
	}

}
